package parser.nodes;

import codegen.CodeGen;
import scanner.Token;

// Expression = SimpleExpression [('=' | '#' | '<' | '<=' | '>' | '>=') SimpleExpression]
public class ExpressionNode extends Node {
    private SimpleExpressionNode left, right;
    private Token comparison;

    public ExpressionNode(SimpleExpressionNode left) {
        this.left = left;
    }

    public ExpressionNode(SimpleExpressionNode left, Token comparison, SimpleExpressionNode right) {
        this.left = left;
        this.right = right;
        this.comparison = comparison;
    }
	
	public void removeCont() {
		if (right == null && comparison == null)
			left.removeCont();
	}

    @Override
    public void print() {
        p("Expression: ");
        indent();
        left.print();
        if(right != null) {
            p(comparison);
            right.print();
        }
        unindent();
    }

    @Override
    public void abstractPrint() {
        if (comparison != null) {
            p(comparison);
            indent();
            left.abstractPrint();
            right.abstractPrint();
            unindent();
        } else {
            left.abstractPrint();
        }
    }

    @Override
    public void compile(CodeGen c) throws CompileError {
         left.compile(c);
         if (right != null) {
            right.compile(c);
            switch(comparison.toString()) {
                case "=":
                    c.outstr("EQ");
                    break;
                case "#":
                    c.outstr("NEQ");
                    break;
                case "<=":
                    c.outstr("LE");
                    break;
                case "<":
                    c.outstr("LT");
                    break;
                case ">=":
                    c.outstr("GE");
                    break;
                case ">":
                    c.outstr("GT");
                    break;
            }
         }
    }
    
    
}
